package system;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;

public class Camera {
	private static float alpha = 0;
	private static float beta = 0;
	private static float r = 0;
	private static GL10 gl;
	
	private static float m_fEyeX = 0;
	private static float m_fEyeY = 0;
	private static float m_fEyeZ = 0;
	
	private static float m_fTargetX	= 0;
	private static float m_fTargetY	= 1;
	private static float m_fTargetZ	= 0;
	
	private static  float m_fUpVectorX 	= 0;
	private static float m_fUpVectorY 	= 1;
	private static float m_fUpVectorZ 	= 0;
	private static float zMin = 1;
	private static float zMax = 20;
	
	private static float s_fRatio;
	
	private static  void SetPerspective()
	{
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluPerspective(gl, 40.0f, s_fRatio, zMin, zMax);
		GLU.gluLookAt(gl, m_fEyeX, m_fEyeY, m_fEyeZ, 
				m_fTargetX, m_fTargetY, m_fTargetZ, 
				m_fUpVectorX, m_fUpVectorY, m_fUpVectorZ);
	}
	
	public static void  SetCamera(GL10 gl, float EyeX, float EyeY, float EyeZ, 
			float TargetX, float TargetY, float TargetZ,
			float UpVectorX, float UpVectorY, float UpVectorZ){
		m_fEyeX = EyeX;
		m_fEyeY = EyeY;
		m_fEyeZ = EyeZ;
		m_fTargetX = TargetX;
		m_fTargetY = TargetY;
		m_fTargetZ = TargetZ;
		m_fUpVectorX = UpVectorX;
		m_fUpVectorY = UpVectorY;
		m_fUpVectorZ = UpVectorZ;
		s_fRatio = Device.GetWidth() / Device.GetHeight();
		SetPerspective();
	}
	
	public static void SetCamera(GL10 Gl, float Alpha, float Beta)
	{		
		gl = Gl;
		alpha = Alpha;
		beta = Beta;
		
		s_fRatio = Device.GetWidth() / Device.GetHeight();
		
		// Look
		r =  (float) Math.sin(Math.PI*alpha / 180);
		m_fTargetY = (float) Math.cos(Math.PI*alpha / 180);
		m_fTargetZ = r * (float) Math.cos(Math.PI*beta / 180);
		m_fTargetX = r * (float) Math.sin(Math.PI*beta / 180);
		
		// Eye
		m_fEyeX = m_fEyeY = m_fEyeZ = 0;
		
		// Up
		m_fUpVectorX = m_fUpVectorZ = 0;
		m_fUpVectorY = 1;
		
		// Eye & Look setup
		SetPerspective();
	}
	
	public static void SetEyePosition(float X, float Y, float Z){
		m_fEyeX = X;
		m_fEyeY = Y;
		m_fEyeZ = Z;
		SetPerspective();
	}
	
	public static void SetTarget(float X, float Y, float Z){
		m_fTargetX = X;
		m_fTargetY = Y;
		m_fTargetZ = Z;
		SetPerspective();
	}
	
	public static void SetUpVector(float X, float Y, float Z){
		m_fUpVectorX = X;
		m_fUpVectorY = Y;
		m_fUpVectorZ = Z;
		SetPerspective();
	}
	
	public static void RotateAlpha(float delAlpha)
	{	
		alpha += delAlpha;
		if (alpha < 0){
			alpha += 360;
		}else if (alpha >= 360){
			alpha -= 360;
		}
		
		r = (float) Math.sin(Math.PI*alpha / 180);
		m_fTargetY = (float) Math.cos(Math.PI*alpha / 180);
		m_fTargetZ = r * (float) Math.cos(Math.PI*beta / 180);
		m_fTargetX = r * (float) Math.sin(Math.PI*beta / 180);
		SetPerspective();
	}
	
	
	public static void RotateBeta(float delBeta)
	{
		beta += delBeta;
		if (beta < 0){
			beta += 360;
		}else if (beta >= 360){
			beta -= 360;
		}
		m_fTargetZ = r * (float) Math.cos(Math.PI*beta / 180);
		m_fTargetX = r * (float) Math.sin(Math.PI*beta / 180);
		SetPerspective();	
	}
	
	public static float GetAlpha (){
		return alpha;
	}
	
	public static float GetBeta (){
		return beta;
	}

	public static void setzMin(float zMin) {
		Camera.zMin = zMin;
	}

	public static float getzMin() {
		return zMin;
	}

	public static void setzMax(float zMax) {
		Camera.zMax = zMax;
	}

	public static float getzMax() {
		return zMax;
	}
}
